﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;
public partial class user_checkout : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            LoadCountry();
            LoadInfoCustomer();
        }
        if (Session["login"] == null)
        {
            ScriptManager.RegisterStartupScript(this, typeof(Page), "Messages", "<script>alert('Please login for check out')</script>", false);
            Response.Redirect("default.aspx");
        }
    }

    protected void LoadCountry()
    {
        List<String> listNode = new List<String>();
        XmlDocument doc = new XmlDocument();
        XmlNodeList nodes;
        doc.Load(HttpContext.Current.Server.MapPath("~/App_Data/countries.xml"));
        nodes = doc.GetElementsByTagName("country");
        foreach (XmlNode node in nodes)
        {

            listNode.Add(node.InnerText.ToString());
        }

        BillDropListCountry.DataSource = listNode;
        BillDropListCountry.DataBind();
        ShipDropDownListCountry.DataSource = listNode;
        ShipDropDownListCountry.DataBind();
    }

    protected void LoadInfoCustomer()
    {
        if (Session["login"] != null)
        {
            String email = Session["email"].ToString();
            String pass = Session["password"].ToString();
            CustomerBus objCBus = new CustomerBus();
            DataTable dt = new DataTable();
            dt = objCBus.getInfoCustomer(email, pass);
            hdIDcustomer.Value = dt.Rows[0]["cus_id"].ToString();
            txtBillFirstName.Text = dt.Rows[0]["cus_firstname"].ToString();
            txtBillLastName.Text = dt.Rows[0]["cus_lastname"].ToString();
            txtBillNumberPhone.Text = dt.Rows[0]["cus_phone"].ToString();
            txtBillEmailAddress.Text = dt.Rows[0]["cus_email"].ToString();
            txtBillAddress1.Text = dt.Rows[0]["cus_address"].ToString();
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (rdShiptothisaddress.Checked)
        {
            System.Random ramdom = new System.Random();
            int codenumer = ramdom.Next(1, 9999);
            Order objO = new Order();
            objO.OdCode = "TNX-" + codenumer;
            objO.CusId = int.Parse(hdIDcustomer.Value.ToString().Trim());
            objO.OrderDate = DateTime.Now;
            objO.Status = "Waiting";
            objO.ShipFname = txtBillFirstName.Text;
            objO.ShipLname = txtBillLastName.Text;
            objO.ShipEmail = txtBillEmailAddress.Text;
            objO.ShipAddress = txtBillAddress1.Text;
            objO.ShipPhone = txtBillNumberPhone.Text;
            objO.ShipState = BillDropListCountry.SelectedItem.Text;
            objO.ShipCity = txtBillCity.Text;
            objO.ShipZipCode = txtBillZipPostalCode.Text;
            objO.PayName = txtPayNamecard.Text;
            objO.PayCardType = PayDropDownListCard.SelectedItem.Text;
            objO.PayCardNum = txtCCNumber.Text;
            objO.PayExpri = DateTime.Parse(txtPaydateExp.Text);
            objO.PayVeriNum = txtCardVerifi.Text;
            objO.Quantity = ShoppingCart.Instance.Items.Count;
            objO.Totalmoney = ShoppingCart.Instance.GetSubTotal();
            OrderBus objOBus = new OrderBus();
            if (objOBus.AddOrder(objO))
            {
                //ScriptManager.RegisterStartupScript(this, typeof(Page), "Messages", "<script>alert('Order successfully')</script>", false);
                Response.Redirect("processcheckout.aspx?cus_id=" + objO.CusId + "&od_code=" + objO.OdCode + "&OrderEmail=" + objO.ShipEmail + "&FirstName=" + objO.ShipFname + "&LastName=" + objO.ShipLname);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, typeof(Page), "Messages", "<script>alert('Order faild!')</script>", false);
            }
        }
        else if (rdShiptodifferentaddress.Checked)
        {
            System.Random ramdom = new System.Random();
            int codenumer = ramdom.Next(1, 9999);
            Order objO = new Order();
            objO.OdCode = "TNX-" + codenumer;
            objO.CusId = int.Parse(hdIDcustomer.Value.ToString().Trim());
            objO.OrderDate = DateTime.Now;
            objO.Status = "Waiting";
            objO.ShipFname = txtShipFirtname.Text;
            objO.ShipLname = txtShipLastname.Text;
            objO.ShipEmail = txtShipEmail.Text;
            objO.ShipAddress = txtShipAddress.Text;
            objO.ShipPhone = txShiptPhonenumber.Text;
            objO.ShipState = ShipDropDownListCountry.SelectedItem.Text;
            objO.ShipCity = txtShipCity.Text;
            objO.ShipZipCode = txtShipZipCode.Text;
            objO.PayName = txtPayNamecard.Text;
            objO.PayCardType = PayDropDownListCard.SelectedItem.Text;
            objO.PayCardNum = txtCCNumber.Text;
            objO.PayExpri = DateTime.Parse(txtPaydateExp.Text);
            objO.PayVeriNum = txtCardVerifi.Text;
            objO.Quantity = ShoppingCart.Instance.Items.Count;
            objO.Totalmoney = ShoppingCart.Instance.GetSubTotal();
            OrderBus objOBus = new OrderBus();
            if (objOBus.AddOrder(objO))
            {
                //ScriptManager.RegisterStartupScript(this, typeof(Page), "Messages", "<script>alert('Order successfully')</script>", false);
                Response.Redirect("processcheckout.aspx?cus_id=" + objO.CusId + "&od_code=" + objO.OdCode + "&OrderEmail=" + objO.ShipEmail + "&FirstName=" + objO.ShipFname + "&LastName=" + objO.ShipLname);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, typeof(Page), "Messages", "<script>alert('Order faild!')</script>", false);
            }
        }
    }
}